let rollTimer = null;

function random(Min,Max){
      var Range = Max - Min;
      var Rand = Math.random();
      var num = Min + Math.round(Rand * Range);
      return num;
}

function rollATag() {
  $(".roll-tag").addClass('active');
  let el = $(".roll-tag span.ready")
  el.addClass('removing');
  setTimeout(() => {
    el.remove();
  }, 100)
  var span = $("<span></span>").text(getRandomTag());
  $(".roll-tag").append(span);
  setTimeout(() => {
    span.addClass('ready');
  }, 100)
}

function rollOnce() {
  clearInterval(rollTimer)
    rollTimer = setInterval(rollATag, 20)
  setTimeout(function() {
    clearInterval(rollTimer)
    rollTimer = setInterval(rollATag, 40)
  }, 400)
  setTimeout(function() {
    clearInterval(rollTimer)
    rollTimer = setInterval(rollATag, 80)
  }, 800)
  setTimeout(function() {
    clearInterval(rollTimer)
    rollTimer = setInterval(rollATag, 140)
  }, 1200)
  setTimeout(function() {
    clearInterval(rollTimer)
    rollTimer = setInterval(rollATag, 240)
  }, 1600)
  setTimeout(function() {
    clearInterval(rollTimer)
    rollTimer = null
  }, 1800)
}

function getRandomTag() {
  // 优先使用从后端传递的标签数据
  if (window.tagsFromServer && window.tagsFromServer.length > 0) {
    return window.tagsFromServer[random(0, window.tagsFromServer.length-1)];
  }
  // 如果后端没有数据，则返回默认标签
  return '标签加载中...';
}

// 存储当前打开的分类信息
let currentOpenCategory = null;
let isContainerVisible = false;

function nav(page, category) {
  switch (page) {
    case 'friends':
      // 使用原生JavaScript
      const gateFriends = document.querySelector('.gate-friends');
      
      // 获取当前要显示的分类选择器
      const categoryClass = category ? 'category-' + category.toLowerCase().replace(/\s+/g, '-') : null;
      
      // 检查是否点击了相同的分类
      if (isContainerVisible && currentOpenCategory === category) {
        // 隐藏容器
        gateFriends.style.display = 'none';
        isContainerVisible = false;
        currentOpenCategory = null;
      } else {
        // 显示容器
        gateFriends.style.display = 'block';
        isContainerVisible = true;
        currentOpenCategory = category;
        
        // 获取所有分类部分
        const allCategories = gateFriends.querySelectorAll('.category-section');
        
        if (category) {
          // 显示特定分类，隐藏其他分类
          allCategories.forEach(section => {
            if (section.classList.contains(categoryClass)) {
              section.style.display = 'block';
            } else {
              section.style.display = 'none';
            }
          });
        } else {
          // 显示所有分类
          allCategories.forEach(section => {
            section.style.display = 'block';
          });
        }
      }
      break;
    
    default:
      break;
  }
}